package com.tdam2011.grupo05.ui.activities.message;

import com.tdam2011.grupo05.R;
import com.tdam2011.grupo05.entities.contact.ContactWeb;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class AddContactWebFragment extends Fragment {

	private static String TAG = "AddContacWeb";

	private static final String[] PROJECTION = new String[] { Contacts._ID,
			Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS,
			Contacts.CONTACT_PRESENCE, Contacts.PHOTO_ID, Contacts.LOOKUP_KEY, };

	private Button mButtonSave;
	private Button mButtonCancel;
	private TextView mTextViewNameApp;
	private TextView mTextViewNameWeb;

	private AddContactCallback mListener;
	private final ContactWeb contactWeb = new ContactWeb();

	public interface AddContactCallback {
		void saveNewContact(ContactWeb c);

		void cancelNewContact();
	}

	@Override
	public void onAttach(Activity activity) {
		Log.d(TAG, "onAttach");
		super.onAttach(activity);
		mListener = (AddContactCallback) activity;

	}

	private View.OnClickListener mSaveAction = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			if (mTextViewNameWeb.getText().toString().equals("")
					|| mTextViewNameWeb.getText().toString().length() < 5) {
				// showDialog;
				((MessageWebActivity) getActivity()).showDialog("Error",
						"Nombre de Contacto inválido o muy corto");
			} else {
				contactWeb.setName(mTextViewNameWeb.getText().toString());
				if (mListener != null) {
					mListener.saveNewContact(contactWeb);
				}
			}
		}
	};

	private View.OnClickListener mCancelAction = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			if (mListener != null) {
				mListener.cancelNewContact();
			}
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = null;

		view = inflater.inflate(R.layout.fragment_add_contact_web, container,
				false);
		mButtonCancel = (Button) view.findViewById(R.id.btn_cancel_new_contact);
		mButtonSave = (Button) view.findViewById(R.id.btn_save_contact_web);
		mTextViewNameApp = (TextView) view
				.findViewById(R.id.text_contact_name_ph);
		mTextViewNameWeb = (TextView) view
				.findViewById(R.id.text_contact_name_web);
		mButtonCancel.setOnClickListener(mCancelAction);
		mButtonSave.setOnClickListener(mSaveAction);

		return view;
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		String uriPicked = "";
		if (getArguments().getString(MessageWebActivity.URI_PICKED) == null) {
			mTextViewNameApp.setEnabled(false);
		} else {
			uriPicked = getArguments().getString(MessageWebActivity.URI_PICKED);
			GetContactPhone task = new GetContactPhone();
			task.execute(uriPicked);
		}

	}

	private class GetContactPhone extends AsyncTask<String, Void, ContactWeb> {

		private String name = "";
		private long idPhone = -1;

		@Override
		protected ContactWeb doInBackground(String... params) {
			Cursor c = getActivity().getContentResolver().query(
					Uri.parse(params[0]), PROJECTION, null, null, null);

			if (c.moveToFirst()) {
				name = c.getString(c
						.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
				idPhone = c.getLong(c
						.getColumnIndex(ContactsContract.Contacts._ID));
				contactWeb.setIdPhone(idPhone);
			}
			c.close();

			return contactWeb;
		}

		@Override
		protected void onPostExecute(ContactWeb result) {
			if (result != null) {
				mTextViewNameApp.setText(name);
				mTextViewNameApp.setEnabled(false);
			} else {
				mTextViewNameApp.setEnabled(false);
			}

		}
	}
}
